//
//  DataHelper.hpp
//  NTESAVDemo
//
//  Created by Sampson on 2024/12/30.
//

#ifndef DataHelper_hpp
#define DataHelper_hpp

#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>


namespace NTES_Stats {

#if 1

template <typename T>
std::vector<T> downsample(const std::vector<T>& input, size_t max_size) {
    size_t n = input.size();
    if (n <= max_size) {
        return input;  // 数据少于等于 max，直接返回
    }

    std::vector<T> result;
    size_t step = n / max_size;  // 计算步长
    size_t remainder = n % max_size;  // 计算余数

    size_t index = 0;
    for (size_t i = 0; i < max_size; ++i) {
        size_t range_size = step + (i < remainder ? 1 : 0);  // 分配额外的点到前 remainder 组
        auto max_it = std::max_element(input.begin() + index, input.begin() + index + range_size);
        result.push_back(*max_it);
        index += range_size;
    }

    return result;
}
//std::vector<int> downsample(const std::vector<int>& input, size_t max_size);

std::string BuildDataset(const std::string& label,
                         const std::vector<int>& data);

std::string BuildMultiDatasets(std::vector<std::pair<std::string, std::vector<int>>>& datasets);

std::string BuildDefaultLabels(int point_count);

std::string TestHTML(std::string chart_id, std::string &labels, std::string &datasets_content, std::string &html_template);

std::string BuildHTML(const std::vector<std::string>& charts_names,
                      const std::vector<std::pair<std::string, std::vector<int>>>& charts, std::string html_template);

#else

std::string BuildDataset(const std::string& label,
                         const std::vector<int>& data,
                         const std::string& borderColor,
                         const std::string& backgroundColor);

std::string BuildMultiDatasets(const std::vector<std::pair<std::string, std::vector<int>>>& datasets,
                               const std::vector<std::pair<std::string, std::string>>& colors);

std::string GenerateMultipleChartsHTML(const std::vector<std::string>& chartIds,
                                       const std::vector<std::vector<std::pair<std::string, std::vector<int>>>>& allDatasets,
                                       const std::vector<std::vector<std::pair<std::string, std::string>>>& allColors);

int TestWriteToFile(std::string &filePath);

#endif

};

#endif /* DataHelper_hpp */
